package org.naxx.security.naxxlet.model;

import org.naxx.security.common.Decision;
import org.naxx.security.common.IndeterminateExpressionException;
import org.naxx.security.model.result.TargetEvaluationResult;

public class XRule {

	protected Decision effect;
	protected XAbstractCondition condition;
	protected XTarget target;
	
	public XRule() {
	}

	public final Decision evaluate() {
		TargetEvaluationResult targetResult = this.target.evaluate();
		if (TargetEvaluationResult.INDETERMINATE.equals(targetResult)) {
			return Decision.INDETERMINATE;
		}
		if (TargetEvaluationResult.NO_MATCH.equals(targetResult)) {
			return Decision.NOT_APPLICABLE;
		}
		try {
			Boolean result = this.condition.evaluate();
			if (Boolean.FALSE.equals(result)) {
				return Decision.NOT_APPLICABLE;
			}
			return this.effect;
		} catch (IndeterminateExpressionException e) {
			return Decision.INDETERMINATE;
		}
	}

	public void setEffect(Decision effect) {
		this.effect = effect;
	}

	public void setCondition(XAbstractCondition condition) {
		this.condition = condition;
	}

	public void setTarget(XTarget target) {
		this.target = target;
	}
}
